<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Product;
use App\Warning;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class WarningController extends Controller
{
    use HasResourceActions;

    protected $title = '基站报警';


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->title)
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->title)
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->title)
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->title)
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Warning);
        $grid->disableCreateButton();

        // $grid->id('Id');
        $grid->model()->orderByDesc('created_at');
        $grid->product()->name('设备名称');
        $states = [
            'on'  => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '未处理', 'color' => 'danger'],
        ];
        $grid->is_handle('是否处理')->switch($states);
        // $grid->handle_at('处理时间')->display(function ($handle_at) {
        //     return $handle_at ?? '未处理';
        // });
        $grid->remark('备注')->display(function ($handle_at) {
            return $handle_at ?? '暂无';
        })->editable('textarea');
        $grid->created_at('发生时间')->display(function ($at) {
            return \Carbon\Carbon::parse($at)->toIntegerMinuteString();
        });

        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->equal('product_id', '设备名称')->select(Product::pluck('name', 'id'));
            $filter->between('created_at', '发生时间')->datetime();
            $filter->equal('is_handle', '是否处理')->radio([
                ''   => '所有',
                0    => '未处理',
                1    => '已处理',
            ]);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Warning::findOrFail($id));

        $show->id('Id');
        $show->product_id('Product id');
        $show->handle_at('Handle at');
        $show->is_handle('Is handle');
        $show->remark('Remark');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Warning);

        $form->number('product_id', 'Product id');
        $form->datetime('handle_at', 'Handle at')->default(date('Y-m-d H:i:s'));
        $states = [
            'on'  => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '未处理', 'color' => 'danger'],
        ];
        $form->switch('is_handle', 'Is handle')->states($states);
        $form->number('remark', 'Remark');
        $form->saving(function (Form $form) {
            if ($form->is_handle == 'on') {
                $form->handle_at = now()->toDateTimeString();
            } else {
                $form->handle_at = null;
            }
        });
        return $form;
    }
}
